package com.huicoo.forestmanager.ui.media;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/9		    Administrator
 * 图片详情页面
 */
public class BrowseImageActivity extends BaseActivity {
    private List<String> imageList;
    private ViewPager vp_browse;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_browse_image;
    }

    @Override
    protected void initView() {
        vp_browse = findViewById(R.id.vp_browse);
        imageList = new ArrayList<>();

        initData();

    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            imageList.add(bundle.getString("imgUrl", ""));
        }
        ImageListAdapter adapter = new ImageListAdapter();
        vp_browse.setAdapter(adapter);
    }

    public class ImageListAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            Context context = container.getContext();
            ImageView childView = new ImageView(context);
            RequestOptions options = new RequestOptions()
                    .fallback(R.drawable.pic_default) //url为空的时候,显示的图片
                    .error(R.drawable.pic_default)
                    .dontAnimate()
                    .override(1280, 720);//图片加载失败后，显示的图片

            Glide.with(context).load(imageList.get(position)).apply(options).into(childView);
            childView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            container.addView(childView);
            return childView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
